package com.hzt.agprosaleplatform.controller;

import com.hzt.agprosaleplatform.common.CustomResponse;
import com.hzt.agprosaleplatform.entity.Order;
import com.hzt.agprosaleplatform.entity.OrderCreateDTO;
import com.hzt.agprosaleplatform.entity.OrderDetailDTO;
import com.hzt.agprosaleplatform.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
@CrossOrigin(origins = "http://localhost:5173")
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderCreateDTO
     * @return
     */
    @PostMapping("/createOrder")
    public CustomResponse createOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        try {
            // 参数校验
            if (orderCreateDTO == null || CollectionUtils.isEmpty(orderCreateDTO.getItems())) {
                return new CustomResponse(500,"订单数据不能为空",null);
            }
            if (StringUtils.isEmpty(orderCreateDTO.getBuyer_id())) {
                return new CustomResponse(500,"买家ID不能为空",null);
            }
            System.out.println(orderCreateDTO.getShop_id());
            if (StringUtils.isEmpty(orderCreateDTO.getShop_id())) {
                return new CustomResponse(500,"供应商ID不能为空",null);
            }
            
            // 创建订单
            Order order = orderService.createOrder(orderCreateDTO);
            return new CustomResponse(200,"OK",order.getOid());
        } catch (Exception e) {
            log.error("创建订单失败", e);
            return new CustomResponse(500,"创建订单失败：" + e.getMessage(),null);
        }
    }

    /**
     * 返回Uid下的所有订单
     * @param uid
     * @return
     */
    @GetMapping("/userOrderList/uid={uid}")
    public CustomResponse UserOrderList(@PathVariable String uid) {
        try {
            List<Order> userorderlist = orderService.selectOrderByUid(uid);
            return new CustomResponse(200,"OK",userorderlist);

        } catch (Exception e) {
            log.error("查询订单列表失败", e);
            return new CustomResponse(500,"查询订单列表失败" + e.getMessage(),null);
        }
    }

    /**
     * 用户订单详情
     * @param uid
     * @return
     */
    @GetMapping("/userOrderDetailedList")
    public CustomResponse UserOrderDetailedList(@RequestParam String uid) {
        try {
            // 参数校验
            if (StringUtils.isEmpty(uid)) {
                return new CustomResponse(500, "用户ID不能为空", null);
            }

            // 获取订单详情列表
            List<OrderDetailDTO> orderDetails = orderService.getOrderDetailsByUid(uid);
            
            // 判断结果
            if (orderDetails == null) {
                return new CustomResponse(404, "未找到订单信息", null);
            }
            
            if (orderDetails.isEmpty()) {
                return new CustomResponse(200, "暂无订单", new ArrayList<>());
            }
            
            return new CustomResponse(200, "OK", orderDetails);
            
        } catch (Exception e) {
            log.error("查询订单详情列表失败", e);
            return new CustomResponse(500, "查询订单详情列表失败: " + e.getMessage(), null);
        }
    }

    /**
     * 商家查询商铺所有订单
     * @param shop_id
     * @return
     */
    @GetMapping("/shopOrderList")
    public CustomResponse ShopOrderList(@RequestParam Integer shop_id){
        try{
            List<Order> shoporderlist = orderService.selectOrderByShopId(shop_id);
            return new CustomResponse(200,"OK",shoporderlist);
        }catch (Exception e){
            log.error("查询订单列表失败", e);
            return new CustomResponse(500,"查询订单列表失败" + e.getMessage(),null);
        }
    }

    /**
     * 更改订单状态
     * @param oid
     * @param status
     * @return
     */
    @PutMapping("/orderStatus")
    public CustomResponse updateOrderStatus(@RequestParam Integer oid,
                                        @RequestParam Integer status) {
        try {
            if (oid == null || status == null) {
                return new CustomResponse(500, "订单ID或状态码不能为空", null);
            }

            boolean updated = orderService.updateOrderStatus(oid, status);
            
            if (!updated) {
                return new CustomResponse(500, "订单不存在或状态更新失败", null);
            }
            
            return new CustomResponse(200, "订单状态更新成功", null);
        } catch (Exception e) {
            log.error("更新订单状态失败", e);
            return new CustomResponse(500, "更新订单状态失败：" + e.getMessage(), null);
        }
    }

    @DeleteMapping("/deleteOrder/oid={oid}")
    public CustomResponse deleteOrder(@PathVariable Integer oid) {
        try {
            return orderService.deleteOrder(oid);
        }catch (Exception e) {
            return new CustomResponse(500,"删除订单操作异常",null);
        }
    }


}
